<?php
namespace app\wxapp\controller;

use app\model\Theme; 
use app\model\Worker; 
use app\model\W_init; 

class WInit extends Base
{
    public function W_init()
    {

    global $_P;
    $fans = $this->checkuser();
    $system=$_G['system'];
    $themeid = intval($_P['themeid']);
    $theme = Theme::getThemeByIdxcx($themeid);
    if(empty($theme['id'])){
        $this->result(1, '活动入口不正确'); 
        exit;
    }
    if (empty($theme['softsn'])) {
        $this->result(1, '软件未激活,请联系' . $_G['companyname'] . '工作人员'); 
    }
    $msg = Worker::gethoutaistats();
    if (!empty($msg)) {
        $this->result(1, $msg); 
    }
    $config =   Theme::getConfig($themeid);

    if ($system['ruanjian']==3){
        $shoplist = db_select ( "SELECT id,name FROM " . tablename (t_shangjia) .
            " WHERE  state = 1 order by displayorder" );
    }

    if ($_P['op']=='next') {
        $result = W_init::pass($system,$config);
        // var_dump( $result);
        if ($result['error']==1) {
            $this->result($result['error'], $result['msg']); 
        }else{
            if ($result['data']['level']=='3') {
                $result = W_init::getteamlist($system);
                $result['data']['level']=3;
            }
            $this->result($result['error'], $result['msg'],$result['data']); 
        }
    }elseif ($_P['op']=='last') {
        W_init::saveteam($system,$config);
        $result = W_init::getteamlist($system);
        $this->result($result['error'], $result['msg'],$result['data']); 
    }elseif ($_P['op']=='save') {
        W_init::saveinfo($fans);

        $myworker = Worker::getThemeWorkerByOpenid($fans['openid'],$themeid,1);
        if ($myworker) {
            if ($myworker['team']>0) {
                $team=db_find("SELECT * FROM ".tablename(t_team)." WHERE id= '{$myworker['team']}'");
                $teamname=$team['name'];
            }
            $result['myworker']=$myworker;
            $result['teamname']=$teamname;
            $this->result(0, '',$result); 
        }else{
            $this->result(1, '注册失败，请重试',''); 
        }
        $result =  array();
    }else{
        $jubf['title'] = empty($theme['jubf'])?'':$theme['jubf'].'-';
        $jubf['banquan'] = !empty($theme['USERCOMPANYNAME'])?$theme['USERCOMPANYNAME']:$_G['companyname'];

        $shareinfo =Theme::getshareinfo($theme,$config,$fans,$shopname,$workername,0);
        $result['shareinfo']=$shareinfo;
        $result['jubf'] = $jubf;
        $result['islogin'] = empty($fans['avatar'])?true:false;
        $result['jishu']=Worker::getjishu($theme,$system);
        $result['shoplist'] = $shoplist;
        $result['ruanjian'] = $system['ruanjian'];
        $this->result(0, '',$result);  
    }
}
}